﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using ST.Torch.WebApi.Application.System.Services;
using Microsoft.Extensions.DependencyInjection;
using Furion.Localization;

namespace ST.Torch.WebApi.Application.AOP
{
   
    /// <summary>
    /// 自定义授权---操作授权检查
    /// </summary>
    public class OperateAuthAttribute : Attribute, IAsyncAuthorizationFilter
    {
        public string key { get; set; }

 

        public OperateAuthAttribute(string Pkey)
        {
       
            key = Pkey;

           
        }
        



        public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            //var _UIDService = App.GetService<IUIDService>();
            // 从context获取IServiceProvider
            IServiceProvider serviceProvider = context.HttpContext.RequestServices;

            // 使用serviceProvider来获取其他服务
            var _UIDService = serviceProvider.GetService<IUIDService>();
            if (!_UIDService.IsExisePermissionId(key))
            {
                // 返回未授权
                throw Oops.Oh($" {L.Text["没有操作权限"]}");

                //context.Result = new UnauthorizedResult();401
            }
            //直接跳过处理

             await Task.CompletedTask;
        }
    }
}
